﻿@model Sohobloo.SoAuth.Web.Models.RoleUsersModel

@section head
{
    @Styles.Render("~/Content/themes/base/css")
}

<h2>Role-User</h2>
@Html.ActionLink("Back", "Index", "Role")
@{
    if(!Model.RoleId.HasValue)
    {
        <div>
            @Html.DropDownListFor(m => m.RoleId, new SelectList(Model.RoleList, "Id", "RoleName"), "Please select...")
        </div>
    }
    else
    {
        using(Html.BeginForm("Add", "RoleUser"))
        {
            @Html.DropDownListFor(m => m.RoleId, new SelectList(Model.RoleList, "Id", "RoleName"))
            @Html.EditorFor(m => m.UserName)
            <input type="submit" value="Add" />
        }
        <table>
            <tr>
                <th>
                    UserName
                </th>
                <th>
                    NickName
                </th>
                <th>
                    Email
                </th>
                <th>
                    UserStatus
                </th>
                <th>
                </th>
            </tr>
            @foreach(var user in Model.RoleUserList)
            {
                <tr>
                    <td>
                        @Html.DisplayFor(m => user.UserName)
                    </td>
                    <td>
                        @Html.DisplayFor(m => user.NickName)
                    </td>
                    <td>
                        @Html.DisplayFor(m => user.Email)
                    </td>
                    <td>
                        @Html.DisplayFor(m => user.UserStatus)
                    </td>
                    <td>
                        @Html.ActionLink("Remove", "Remove", new
                        {
                            id = Model.RoleId, userId = user.Id
                        })
                    </td>
                </tr>
            }
        </table>
    }
}

@section scripts
{
    @Scripts.Render("~/bundles/jqueryui")
    <script>
        $().ready(function() {
            var $UserName = $("#UserName");
            $UserName.autocomplete({
                source: function(request, response) {
                    $.ajax({
                        url: "@Url.Action("Search", "User")",  //要查询的Action
                        type: "POST",  //Post提交
                        dataType: "json", //json格式数据，默认是text
                        data: { keyword: request.term }, //参数
                        success: function(data) {
                            response(data);
                        }
                    });
                }
            });

            var $RoleList = $("#RoleId");
            $RoleList.change(function() {
                var roleId = $RoleList.val();
                if (!roleId) return;
                window.location = "/RoleUser/Index/" + roleId;
            });
        });
    </script>
}